#ifndef SERIAL_CONNETCT_H
#define SERIAL_CONNETCT_H

//this class is used to connect turntable controller

#include<QObject>
#include<QtCore>
#include<QModbusClient>
#include<QModbusDataUnit>
#include<QModbusTcpClient>
#include<QModbusRtuSerialMaster>
#include<QObject>
#include<QEventLoop>
#include<QTimer>
#include<QSerialPort>
#include<QDebug>
#include<QVector>
#include<QMessageBox>

struct ModBSettings {
//    this is used to setting connect parameter;
    int parity = QSerialPort::NoParity;
    int baud = QSerialPort::Baud9600;
    int dataBits = QSerialPort::Data8;
    int stopBits = QSerialPort::OneStop;
    int responseTime = 5000;
    int numberOfRetries = 3;
    QString port="port1"; //this part could be changed according to PC serial port or resetting.
    void infoshow();
};

enum Modbus_flag {Init,Speed,Angle,Run,Stop,Zero,Draft};

class Serial_connect : public QObject
{
    Q_OBJECT
public:
    explicit Serial_connect(QObject *parent = nullptr);

    short getStage_count() const;
    void setStage_count(short value);

    Modbus_flag getCurrent_state() const;
    void setCurrent_state(const Modbus_flag &current_state);

    bool getIs_smallTable() const;
    void setIs_smallTable(bool value);

signals:

    void startInfo(Modbus_flag flag,QVector<QString> vec,bool is_small_one);
    void sendError(QString errorInfo);

public slots:

    bool MB_connect();/*to connetct the Controller*/
    void MB_disconnect();/*to disconnect the Controller*/
    void set_port_settings(ModBSettings m_port_set); //set port setting.
    void Deal_signal(Modbus_flag flag, QVector<QString> vec,bool is_small_one);//deal turntable detaill
    void readReady(); //deal with read commander
    void writefinishReady(); //deal with commander after write finish
private:
    QModbusClient *m_modbus;

    ModBSettings m_MBSetting;

    QMutex mutex;

    short stage_count; //modbus common counter

    Modbus_flag m_current_state;

    bool is_smallTable;

};

#endif // SERIAL_CONNETCT_H
